#include <QSpinBox>
#include <QLabel>
#include <QHBoxLayout>
#include <QKeyEvent>

#include "QyGotoDialog.h"

QyGotoDialog::QyGotoDialog( QWidget * parent ) : QDialog(parent) {

	QHBoxLayout * layout = new QHBoxLayout(this);
	QLabel * label = new QLabel( tr("Line number:"), this );
	valueEdit = new QSpinBox(this);
	rangeLabel = new QLabel(this);

	valueEdit->setMinimum(1);
	// valueEdit->setMinimumWidth(300);

	layout->addWidget(label);
	layout->addWidget(valueEdit);
	layout->addWidget(rangeLabel);

	setWindowTitle( tr("Goto dialog") );

}

void QyGotoDialog::setMaximum( int value ) {

	valueEdit->setMaximum(value);
	rangeLabel->setText( tr("1-%2").arg(value) );

}

void QyGotoDialog::keyPressEvent( QKeyEvent * event ) {

	int key = event->key(),
		modifiers = event->modifiers();

	if ( key == Qt::Key_Return && modifiers == Qt::NoModifier ) {
		close();
		emit initGoto( valueEdit->value() );
	} else if ( key == Qt::Key_Escape && modifiers == Qt::NoModifier )
		close();

}
